﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessFacade;
using Entities;
using Comm;

namespace MedicareNoteManagerWeb.Web
{
    public partial class MainFunction : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PageGrid1_Invalidate(sender, e);
                PageGrid1_SelectedValueChanged(sender, e);
            }
        }

        protected void PageGrid1_Invalidate(object sender, EventArgs e)
        {
            DataTable dt = Busi_sys_Menu.Getlist();
            this.PageGrid1.BindFullDataTable(dt, true);
        }

        protected void PageGrid1_SelectedValueChanged(object sender, EventArgs e)
        {
            txtModuleName.Text = Busi_sys_Menu.GetModel(PageGrid1.SelectedValue == null ? 0 : int.Parse(PageGrid1.SelectedValue));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PageGrid1.ResetState();
            txtModuleName.Text = "";
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (Busi_sys_Menu.Delete(int.Parse(PageGrid1.SelectedValue)))
                {
                    MessageBox.ShowDialog(Page, "删除成功！", "success");
                    //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('删除成功！'); </script>");
                    PageGrid1_Invalidate(sender, e);
                    PageGrid1_SelectedValueChanged(sender, e);
                }
                else
                {
                    //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('删除失败！'); </script>");
                    MessageBox.ShowDialog(Page, "删除失败！", "error");
                    PageGrid1_Invalidate(sender, e);
                    PageGrid1_SelectedValueChanged(sender, e);
                }
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        public void GetModel(sys_menu_Model model)
        {
            model.MenuID = PageGrid1.SelectedValue == null ? 0 : int.Parse(PageGrid1.SelectedValue);
            model.MenuName = txtModuleName.Text.Trim();
            model.MenuParentNo = 0;
        }
        public string CheckData()
        {
            string strErr = "";
            if (string.IsNullOrEmpty(txtModuleName.Text.Trim()))
            {
                strErr += "功能模块名称不能为空";
            }
            return strErr;
        }
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            sys_menu_Model model = new sys_menu_Model();
            GetModel(model);
            if (!string.IsNullOrEmpty(CheckData()))
            {
                MessageBox.ShowDialog(Page, CheckData(), "error");
                //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('" + CheckData() + "'); </script>");
            }
            else
            {
                if (PageGrid1.SelectedValue == null)
                {
                    if (Busi_sys_Menu.Add(model))
                    {

                        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('添加成功！'); </script>");
                        MessageBox.ShowDialog(Page, "添加成功！", "success");
                        PageGrid1_Invalidate(sender, e);
                        PageGrid1_SelectedValueChanged(sender, e);
                    }
                    else
                    {
                        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('添加失败！'); </script>");
                        MessageBox.ShowDialog(Page, "添加失败！", "error");
                        PageGrid1_Invalidate(sender, e);
                        PageGrid1_SelectedValueChanged(sender, e);
                    }
                }
                else
                {
                    try
                    {
                        Busi_sys_Menu.Update(model);
                        MessageBox.ShowDialog(Page, "更新成功！", "success");
                        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.success('更新成功！'); </script>");
                        PageGrid1_Invalidate(sender, e);
                        PageGrid1_SelectedValueChanged(sender, e);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.ShowDialog(Page, "更新失败！" + ex.Message, "error");
                        //Response.Write("<script language='javascript' type='text/javascript'>  $.ligerDialog.error('更新失败！" + ex.Message + "'); </script>");
                        PageGrid1_Invalidate(sender, e);
                        PageGrid1_SelectedValueChanged(sender, e);
                    }
                }
            }

        }

    }
}